package com.xiaochong.bitong.job.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xc.logclient.utils.LogTrace;
import com.xiaochong.bitong.job.config.hbase.HbaseBaseQueryComponent;
import com.xiaochong.bitong.job.util.StringUtil;
import com.xiaochong.tir.common.data.hbasepo.BroaderIndex;
import com.xiaochong.tir.common.data.hbasepo.ConceptIndex;
import com.xiaochong.tir.common.data.vo.ChartVo;
import com.xiaochong.tir.common.data.vo.GaiNianYVo;
import com.xiaochong.tir.common.data.vo.GaiNianZhiShuVo;
import com.xiaochong.tir.common.exception.hbase.HbaseCloseExcetion;
import com.xiaochong.tir.common.exception.hbase.HbaseConnectionExcetion;
import com.xiaochong.tir.common.exception.hbase.HbaseGetTableExcetion;
import com.xiaochong.tir.common.util.DateUtils;
import com.xiaochong.tir.common.util.HbaseTableUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by Administrator on 2018/8/14.
 */
@Service
public class ConceptService {
    @Autowired
    HbaseBaseQueryComponent hbaseBaseQueryComponent;

    /**
     * 概念指数(列表)
     * @param rowKey
     * @return
     * @throws HbaseConnectionExcetion
     * @throws HbaseGetTableExcetion
     * @throws IOException
     * @throws HbaseCloseExcetion
     */
    private String getBroaderData(String rowKey) throws HbaseConnectionExcetion, HbaseGetTableExcetion, IOException, HbaseCloseExcetion {
        String grailStr = hbaseBaseQueryComponent.selectByQualifierData(HbaseTableUtils.CONCEPTUAL_QUOTES_TABLE_NAME
                , HbaseTableUtils.CONCEPTUAL_QUOTES_TABLE_NAME_FAMILY
                , HbaseTableUtils.CONCEPTUAL_QUOTES_TABLE_NAME_QUALIFIER
                , rowKey
        );
        LogTrace.info("hbase概念指数查询","是否存在数据"+rowKey, StringUtils.isBlank(grailStr)?"无数据":"有数据");
        return grailStr;
    }


    /**
     * 概念行情指数一年的数据
     * @return
     * @throws HbaseCloseExcetion
     * @throws IOException
     * @throws HbaseGetTableExcetion
     * @throws HbaseConnectionExcetion
     */
    public GaiNianZhiShuVo getConceptualQuotesOfYear(int id) throws HbaseCloseExcetion, IOException, HbaseGetTableExcetion, HbaseConnectionExcetion {

        int day=365;
        List<String> x=new ArrayList<>();
        List<GaiNianYVo> voList=new ArrayList<>();
        GaiNianZhiShuVo vo=new GaiNianZhiShuVo();
        for(int k=0;k<day;k++){
            String rowkey = id+"@"+DateUtils.getNextDayDateYMD(-k);
            String grailStr =getBroaderData(rowkey);
            JSONObject json = JSON.parseObject(grailStr);
                JSONArray advice =null;
                if (json!=null) {
                    advice = (JSONArray) json.getJSONArray( 0+"" );
                }
                if (advice!=null) {
                        JSONObject obj = (JSONObject) advice.get(0);
                    if(obj!=null&&obj.containsKey("conceptIndex")&&Double.valueOf(obj.get("conceptIndex").toString())<2000) {
                        GaiNianYVo gaiNianYVo = new GaiNianYVo();
                        if (obj.containsKey( "createTime" )) {
                            x.add( obj.get( "createTime" ).toString() );
                        }
                        if (obj.containsKey( "conceptIndex" ))
                            gaiNianYVo.setValue( obj.get( "conceptIndex" ).toString() );
                        if (gaiNianYVo.getValue() == null) {
                            gaiNianYVo.setValue( 0 + "" );
                        }
                        if (obj.containsKey( "avgPercentChange24h" ))
                            gaiNianYVo.setChange( obj.get( "avgPercentChange24h" ).toString() );
                        gaiNianYVo.setDifference( StringUtil.getDoubleTwo( Double.parseDouble( gaiNianYVo.getValue() ) * Double.parseDouble( gaiNianYVo.getChange() ) / 100 ) + "" );
                        voList.add( gaiNianYVo );
                    }
                }
        }

        Collections.reverse(x);
        Collections.reverse(voList);
        vo.setX(x);
        vo.setY(voList);
        return vo;
    }

    /**
     * 概念行情指数30天的数据
     * @return
     * @throws HbaseCloseExcetion
     * @throws IOException
     * @throws HbaseGetTableExcetion
     * @throws HbaseConnectionExcetion
     */
    public GaiNianZhiShuVo getConceptualQuotesOf30Days(int id) throws HbaseCloseExcetion, IOException, HbaseGetTableExcetion, HbaseConnectionExcetion {
        int day=30;
        int hour=24;
        int hourStrInt = getNowHour();
        List list=new ArrayList();
        List<String> x=new ArrayList<>();
        List<GaiNianYVo> voList=new ArrayList<>();
        GaiNianZhiShuVo vo=new GaiNianZhiShuVo();
        for(int k=0;k<=day;k++){
            String rowkey = id+"@"+DateUtils.getNextDayDateYMD(-k);
            String grailStr =getBroaderData(rowkey);
            JSONObject json = JSON.parseObject(grailStr);
            for (int i=hour-1;i>=0;i--){
                JSONArray advice =null;
                if (json!=null) {
                    advice = (JSONArray) json.getJSONArray( i+"" );
                    i=i-3;
                }
                if (advice!=null) {
                    JSONObject obj = (JSONObject) advice.get(0);
                    if(obj!=null&&obj.containsKey("conceptIndex")&&Double.valueOf(obj.get("conceptIndex").toString())<2000) {
                        GaiNianYVo gaiNianYVo = new GaiNianYVo();
                        if (obj.containsKey( "createTime" )) {
                            x.add( obj.get( "createTime" ).toString() );
                        }
                        if (obj.containsKey( "conceptIndex" ))
                            gaiNianYVo.setValue( obj.get( "conceptIndex" ).toString() );
                        if (gaiNianYVo.getValue() == null) {
                            gaiNianYVo.setValue( 0 + "" );
                        }
                        if (obj.containsKey( "avgPercentChange24h" ))
                            gaiNianYVo.setChange( obj.get( "avgPercentChange24h" ).toString() );
                        gaiNianYVo.setDifference( StringUtil.getDoubleTwo( Double.parseDouble( gaiNianYVo.getValue() ) * Double.parseDouble( gaiNianYVo.getChange() ) / 100 ) + "" );
                        voList.add( gaiNianYVo );
                    }
                }
            }
        }

        Collections.reverse(x);
        Collections.reverse(voList);
        vo.setX(x);
        vo.setY(voList);
        list.add(vo);
        return vo;
    }

    /**
     * 概念行情指数7天的数据
     * @return
     * @throws HbaseCloseExcetion
     * @throws IOException
     * @throws HbaseGetTableExcetion
     * @throws HbaseConnectionExcetion
     */
    public GaiNianZhiShuVo getConceptualQuotesOf7Days(int id) throws HbaseCloseExcetion, IOException, HbaseGetTableExcetion, HbaseConnectionExcetion {
        int day=7;
        int hour=24;
        List list=new ArrayList();
        List<String> x=new ArrayList<>();
        List<GaiNianYVo> voList=new ArrayList<>();
        GaiNianZhiShuVo vo=new GaiNianZhiShuVo();
        for(int k=0;k<=day;k++){
            String rowkey = id+"@"+DateUtils.getNextDayDateYMD(-k);
            String grailStr =getBroaderData(rowkey);
            JSONObject json = JSON.parseObject(grailStr);
            for (int i=hour-1;i>=0;i--){
                JSONArray advice =null;
                if (json!=null) {
                    advice = (JSONArray) json.getJSONArray( i+"" );
                }
                if (advice!=null) {
                        JSONObject obj = (JSONObject) advice.get(0);
                        if(obj!=null&&obj.containsKey("conceptIndex")&&Double.valueOf(obj.get("conceptIndex").toString())<2000){
                            GaiNianYVo gaiNianYVo=new GaiNianYVo();
                            if (obj.containsKey("createTime")) {
                                x.add(obj.get("createTime").toString());
                            }
                            if (obj.containsKey("conceptIndex")) gaiNianYVo.setValue(obj.get("conceptIndex").toString());
                            if(gaiNianYVo.getValue()==null){
                                gaiNianYVo.setValue( 0+"" );
                            }
                            if (obj.containsKey("avgPercentChange24h")) gaiNianYVo.setChange(obj.get("avgPercentChange24h").toString());
                            gaiNianYVo.setDifference(  StringUtil.getDoubleTwo(Double.parseDouble(gaiNianYVo.getValue())*Double.parseDouble(gaiNianYVo.getChange())/100)+"");
                            voList.add(gaiNianYVo);
                        }
                }
            }
        }

        Collections.reverse(x);
        Collections.reverse(voList);
        vo.setX(x);
        vo.setY(voList);
        list.add(vo);
        return vo;
    }


    /**
     * 概念行情指数1天的数据
     * @return
     * @throws HbaseCloseExcetion
     * @throws IOException
     * @throws HbaseGetTableExcetion
     * @throws HbaseConnectionExcetion
     */
    public GaiNianZhiShuVo getConceptualQuotesOfOneDays(int id) throws HbaseCloseExcetion, IOException, HbaseGetTableExcetion, HbaseConnectionExcetion {
        int day=1;
        int hour=24;
        int hourStrInt = getNowHour();
        List list=new ArrayList();
        List<String> x=new ArrayList<>();
        List<GaiNianYVo> voList=new ArrayList<>();
        GaiNianZhiShuVo vo=new GaiNianZhiShuVo();
        for(int k=0;k<=day;k++){
            String rowkey = id+"@"+DateUtils.getNextDayDateYMD(-k);
            String grailStr =getBroaderData(rowkey);
            JSONObject json = JSON.parseObject(grailStr);
            for (int i=0;i<hour;i++){
                JSONArray advice =null;
                if (json!=null) {
                    int nowHour = hourStrInt;
                    if(hourStrInt == 0){
                        hourStrInt = 23;
                    }else{
                        hourStrInt--;
                    }
                    advice = (JSONArray) json.getJSONArray( nowHour+"" );
                }
                if (advice!=null) {
                    for(int j = advice.size()-1; j >=0; j--){
                        JSONObject obj = (JSONObject) advice.get(j);
                        GaiNianYVo gaiNianYVo=new GaiNianYVo();
                        if (obj.containsKey("createTime")) {
                            x.add(obj.get("createTime").toString());
                        }
                        if (obj.containsKey("conceptIndex")) gaiNianYVo.setValue(obj.get("conceptIndex").toString());
                        if(gaiNianYVo.getValue()==null){
                            gaiNianYVo.setValue( 0+"" );
                        }
                        if (obj.containsKey("avgPercentChange24h")) gaiNianYVo.setChange(obj.get("avgPercentChange24h").toString());
                        gaiNianYVo.setDifference(  StringUtil.getDoubleTwo(Double.parseDouble(gaiNianYVo.getValue())*Double.parseDouble(gaiNianYVo.getChange())/100)+"");
                        voList.add(gaiNianYVo);
                        j--;
                    }
                }
            }
        }

        Collections.reverse(x);
        Collections.reverse(voList);
        vo.setX(x);
        vo.setY(voList);
        list.add(vo);
        return vo;
    }


    /**
     * 概念行情指数3小时的数据
     * @return
     * @throws HbaseCloseExcetion
     * @throws IOException
     * @throws HbaseGetTableExcetion
     * @throws HbaseConnectionExcetion
     */
    public GaiNianZhiShuVo getConceptualQuotesOf3Hours(int id) throws HbaseCloseExcetion, IOException, HbaseGetTableExcetion, HbaseConnectionExcetion {
        int day=1;
        int hour=3;
        int hourStrInt = getNowHour();
        List list=new ArrayList();
        List<String> x=new ArrayList<>();
        List<GaiNianYVo> voList=new ArrayList<>();
        GaiNianZhiShuVo vo=new GaiNianZhiShuVo();

        Map<String,String> map=new HashMap<>(  );
        for (int j=0;j<=day;j++){
            String rowkey = DateUtils.getNextDayDateYMD(-j);//取rowkey
            map.put( rowkey,rowkey );
        }

        for (String key : map.keySet()) {
            String rowkey = id+"@"+key;
            String grailStr = getBroaderData(rowkey);
            map.put( key,grailStr);
        }

        for (int j = 0; j < hour; j++) {
            String nowRowKey = DateUtils.getLastHourDateStrOfDate( j );
            String grailStr = map.get( nowRowKey );
            JSONObject json = JSON.parseObject( grailStr );

            int nowHour = hourStrInt;
            if (hourStrInt == 0) {
                hourStrInt = 23;
            } else {
                hourStrInt--;
            }
            JSONArray advice = null;

            if (json != null) {
                advice = (JSONArray) json.getJSONArray( nowHour + "" );
                if (advice!=null) {
                    for(int k = advice.size()-1; k >=0; k--){
                        JSONObject obj = (JSONObject) advice.get(k);
                        GaiNianYVo gaiNianYVo=new GaiNianYVo();
                        if (obj.containsKey("createTime")) {
                            x.add(obj.get("createTime").toString());
                        }
                        if (obj.containsKey("conceptIndex")) gaiNianYVo.setValue(obj.get("conceptIndex").toString());
                        if(gaiNianYVo.getValue()==null){
                            gaiNianYVo.setValue( 0+"" );
                        }
                        if (obj.containsKey("avgPercentChange24h")) gaiNianYVo.setChange(obj.get("avgPercentChange24h").toString());
                        gaiNianYVo.setDifference(  StringUtil.getDoubleTwo(Double.parseDouble(gaiNianYVo.getValue())*Double.parseDouble(gaiNianYVo.getChange())/100)+"");
                        voList.add(gaiNianYVo);
                    }
                }
            }
        }

        Collections.reverse(x);
        Collections.reverse(voList);
        vo.setX(x);
        vo.setY(voList);
        list.add(vo);
        return vo;
    }

    public static int getNowHour(){
        String nowDateHoursStr = DateUtils.dateFormatString(new Date(),DateUtils.format_ymdh_x);
        String hourStr = nowDateHoursStr.substring(nowDateHoursStr.length()-2,nowDateHoursStr.length());
        return Integer.valueOf(hourStr);
    }
}
